package cn.danger.com.fragment.user;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import cn.danger.com.activity.CommonActivity;
import cn.danger.com.activity.R;
import cn.danger.com.fragment.BackHandledFragment;
import cn.danger.com.presenter.ModifyPasswordPresenter;
import cn.danger.com.presenter.ModifyPasswordPresenterImpl;
import cn.danger.com.util.Constants;
import cn.danger.com.view.CommonView;
import cn.danger.com.widget.ClearEditText;

/**
 * Created by 俊 on 2016/12/29.
 */
public class ModifyPasswordFragment extends BackHandledFragment implements CommonView {

    private boolean hadIntercept;
    private View view;
    private CommonActivity activity;
    private Toolbar toolbar;
    private ClearEditText origin_password_editText;
    private ClearEditText new_password_editText;
    private ClearEditText confirm_password_editText;
    private ProgressDialog dialog;
    private ModifyPasswordPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_modify_password, container, false);

        bindView();
        bindEvent();

        return view;
    }

    @Override
    public void bindView() {
        activity = (CommonActivity) getActivity();
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolbar.setTitle("");
        activity.setSupportActionBar(toolbar);
        origin_password_editText = (ClearEditText) view.findViewById(R.id.origin_password_editText);
        new_password_editText = (ClearEditText) view.findViewById(R.id.new_password_editText);
        confirm_password_editText = (ClearEditText) view.findViewById(R.id.confirm_password_editText);

        dialog = new ProgressDialog(activity, ProgressDialog.THEME_HOLO_LIGHT);
        dialog.setMessage("正在执行...");
        dialog.setCanceledOnTouchOutside(false);

        presenter = new ModifyPasswordPresenterImpl(activity, this);
    }

    @Override
    public void bindEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.dismiss();
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
        if (message.equals(Constants.SUCCESS_TO_UPDATE_PASSWORD)) {
            getFragmentManager().popBackStack();
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.modify_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.save) {
            presenter.updatePassword(origin_password_editText.getText().toString(),
                    new_password_editText.getText().toString(), confirm_password_editText.getText().toString());
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }


}
